<template>
  <div>
    <!-- 列表 -->
    <ul class="TemplateStyle">
      <!-- 采购日期 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/date.png" />
          <span class="li_txt">采购日期</span>
        </div>
        <div class="label">
          <input type="text" :value="userDate" :disabled="true" />
        </div>
      </li>
      <!-- 支付爱心码 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/loveCode.png" />
          <span class="li_txt">支付爱心码</span>
        </div>
        <div class="label">
          <input type="number" v-model="donateId" />
        </div>
      </li>
      <!-- 采购物资(名称) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/birth.png" />
          <span class="li_txt">采购物资(名称)</span>
        </div>
        <div class="label">
          <input type="text" v-model="buyName" />
        </div>
      </li>
      <!-- 采购物资(单位) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/order.png" />
          <span class="li_txt">采购物资(单位)</span>
        </div>
        <div class="label">
          <input type="text" v-model="buyUnit" />
        </div>
      </li>
      <!-- 采购物资(数量) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/money.png" />
          <span class="li_txt">采购物资(数量)</span>
        </div>
        <div class="label">
          <input type="number" v-model="buyNum" />
        </div>
      </li>
      <!-- 供应单位 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/transport.png" />
          <span class="li_txt">供应单位</span>
        </div>
        <div class="label">
          <input type="text" v-model="supportDept" />
        </div>
      </li>
      <!-- 物资单价(元) -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/money.png" />
          <span class="li_txt">物资单价(元)</span>
        </div>
        <div class="label">
          <input type="number" v-model="buyPrice" />
        </div>
      </li>
      <!-- 操作人 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/donation.png" />
          <span class="li_txt">操作人</span>
        </div>
        <div class="label">
          <input type="text" v-model="operator" />
        </div>
      </li>
      <!-- 附件 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/Enclosure.png" />
          <span class="li_txt">附件</span>
        </div>
        <van-uploader accept="image/png, image/jpeg, image/jpg" :after-read="afterRead">
          <van-button
            style="border-radius:.8rem;height:2.1rem;line-height:2.1rem"
            icon="photo"
            type="primary"
          >上传领用原始凭证</van-button>
        </van-uploader>
      </li>
      <!-- 确认按钮 -->
      <button class="btn" @click="btnPurchase">确定</button>
    </ul>
    <!-- loading -->
    <div style="padding-bottom: 4rem; position:top:50%">
      <van-loading
        v-show="loading"
        style="position: absolute;top: 50%;left: 46%;"
        type="spinner"
        color="#1989fa"
      ></van-loading>
    </div>
  </div>
</template>
<script>
import moment from "moment";

export default {
  name: "PurchaseMaterials",
  data() {
    return {
      userDate: "", // 展示给用户
      buyTime: "",
      donateId: "",
      buyName: "",
      buyUnit: "",
      buyNum: "",
      supportDept: "",
      buyPrice: "",
      operator: "",
      img: "",
      loading: false // loading
    };
  },
  created() {
    // 自动生成日期
    let nowDate = new Date();
    this.buyTime = moment(nowDate).format("YYYY-MM-DD HH:mm:ss");
    this.userDate = moment(nowDate).format("YYYY-MM-DD");
  },
  methods: {
    afterRead(file) {
      this.img = file.content;
    },
    btnPurchase() {
      this.loading = true; // loading显示
      let params = {
        token: JSON.parse(localStorage.getItem("login")).res.token,
        userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
        buyTime: this.buyTime,
        donateId: this.donateId,
        buyName: this.buyName,
        buyUnit: this.buyUnit,
        buyNum: this.buyNum,
        supportDept: this.supportDept,
        buyPrice: this.buyPrice,
        operator: this.operator,
        attachment: this.img
      };
      this.$api
        .requestbuyProduct(params)
        .then(res => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: res
          });
        })
        .catch(err => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: err
          });
        });
    }
  }
};
</script>
<style>
@import "../../assets/css/TemplateStyle.css";
</style>
